package com.kly.sc.api.application.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Maps;
import com.kly.user.enums.AppCodeEnum;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AppConfigs {

    @NacosValue(value = "${app.login.channel}", autoRefreshed = true)
    private String loginChannel;
    @NacosValue(value = "${app.login.channel.new}", autoRefreshed = true)
    private String loginChannelNew;
    @NacosValue(value = "${app.login.channel.touchu}", autoRefreshed = true)
    private String touchuLoginChannel;
    @NacosValue(value = "${app.login.channel.pp}", autoRefreshed = true)
    private String ppLoginChannel;
    @NacosValue(value = "${pp.pv}", autoRefreshed = true)
    private String ppAndPvSwitch;
    @NacosValue(value = "${touchu.pp.pv}", autoRefreshed = true)
    private String touchuPpAndPvSwitch;
    @NacosValue(value = "${pp.pp.pv}", autoRefreshed = true)
    private String ppPpAndPvSwitch;
    @NacosValue(value = "${turn.ons}", autoRefreshed = true)
    private String turnOnsSwitch;
    @NacosValue(value = "${touchu.turn.ons}", autoRefreshed = true)
    private String touchuTurnOnsSwitch;
    @NacosValue(value = "${pp.turn.ons}", autoRefreshed = true)
    private String ppTurnOnsSwitch;
    @NacosValue(value = "${flashchat.messages}", autoRefreshed = true)
    private String flashchatMessages;
    @NacosValue(value = "${touchu.flashchat.messages}", autoRefreshed = true)
    private String touchuFlashchatMessages;
    @NacosValue(value = "${pp.flashchat.messages}", autoRefreshed = true)
    private String ppFlashchatMessages;
    @NacosValue(value = "${homepage.bgm}", autoRefreshed = true)
    private String homepageBgm;
    @NacosValue(value = "${touchu.homepage.bgm}", autoRefreshed = true)
    private String touchuHomepageBgm;
    @NacosValue(value = "${pp.homepage.bgm}", autoRefreshed = true)
    private String ppHomepageBgm;
    @NacosValue(value = "${app.configs.agreement.privacyPolicy}", autoRefreshed = true)
    private String privacyPolicyUrl;
    @NacosValue(value = "${app.configs.agreement.terms}", autoRefreshed = true)
    private String terms;
    @NacosValue(value = "${app.configs.agreement.paymentAgreement}", autoRefreshed = true)
    private String paymentAgreement;

    @NacosValue(value = "${app.configs.agreement.ios-privacyPolicy}", autoRefreshed = true)
    private String iosPrivacyPolicyUrl;
    @NacosValue(value = "${app.configs.agreement.ios-terms}", autoRefreshed = true)
    private String iosTerms;
    @NacosValue(value = "${app.configs.agreement.ios-paymentAgreement}", autoRefreshed = true)
    private String iosPaymentAgreement;

    @NacosValue(value = "${touchu.configs.agreement.privacyPolicy}", autoRefreshed = true)
    private String touchuPrivacyPolicyUrl;
    @NacosValue(value = "${pp.configs.agreement.privacyPolicy}", autoRefreshed = true)
    private String ppPrivacyPolicyUrl;
    @NacosValue(value = "${touchu.configs.agreement.terms}", autoRefreshed = true)
    private String touchuTerms;
    @NacosValue(value = "${pp.configs.agreement.terms}", autoRefreshed = true)
    private String ppTerms;
    @NacosValue(value = "${touchu.configs.agreement.paymentAgreement}", autoRefreshed = true)
    private String touchuPaymentAgreement;
    @NacosValue(value = "${pp.configs.agreement.paymentAgreement}", autoRefreshed = true)
    private String ppPaymentAgreement;
    @NacosValue(value = "${switch.manage.admin:false}", autoRefreshed = true)
    private boolean switchManageAdmin;
    @NacosValue(value = "${touchu.switch.manage.admin:false}", autoRefreshed = true)
    private boolean touchuSwitchManageAdmin;
    @NacosValue(value = "${pp.switch.manage.admin:false}", autoRefreshed = true)
    private boolean ppSwitchManageAdmin;
    @NacosValue(value = "${customer.service.userCode}")
    private String customerServiceUserCode;
    @NacosValue(value = "${touchu.customer.service.userCode}")
    private String touchuCustomerServiceUserCode;
    @NacosValue(value = "${pp.customer.service.userCode}")
    private String ppCustomerServiceUserCode;
    @NacosValue(value = "${unlimit.chat.message.num:99999}")
    private Integer unLimitChatMessageNum;
    @NacosValue(value = "${user.flashchat.default.msg:You summoned a Flash Chat.}")
    private String userSendFlashChatMsg;
    @NacosValue(value = "${touchu.user.flashchat.default.msg:You summoned a Flash Chat.}")
    private String touchuUserSendFlashChatMsg;
    @NacosValue(value = "${pp.user.flashchat.default.msg:You summoned a Flash Chat.}")
    private String ppUserSendFlashChatMsg;
    @NacosValue(value = "${model.flashchat.default.msg:You got hit with a Flash Chat.}")
    private String modelSendFlashChatMsg;
    @NacosValue(value = "${touchu.model.flashchat.default.msg:You got hit with a Flash Chat.}")
    private String touchuModelSendFlashChatMsg;
    @NacosValue(value = "${pp.model.flashchat.default.msg:You got hit with a Flash Chat.}")
    private String ppModelSendFlashChatMsg;
    @NacosValue(value = "${default.msg:Start Chatting}")
    private String defaultChatMsg;
    @NacosValue(value = "${default.msg:Start Chatting}")
    private String touchuDefaultChatMsg;
    @NacosValue(value = "${pp.default.msg:Start Chatting}")
    private String ppDefaultChatMsg;
    @NacosValue(value = "${default.portrait_uri}", autoRefreshed = true)
    private String defaultPortraitUrl;
    @NacosValue(value = "${touchu.default.portrait_uri}", autoRefreshed = true)
    private String touchuDefaultPortraitUrl;
    @NacosValue(value = "${pp.default.portrait_uri}", autoRefreshed = true)
    private String ppDefaultPortraitUrl;
    @NacosValue(value = "${chatter.url:http://localhost:9105}")
    private String chatterApiUrl;
    @NacosValue(value = "${countdown.delay.time:5400}", autoRefreshed = true)
    private Long countDownDelayTime;
    @NacosValue(value = "${touchu.countdown.delay.time:5400}", autoRefreshed = true)
    private Long touchuCountDownDelayTime;
    @NacosValue(value = "${touchu.countdown.delay.time:5400}", autoRefreshed = true)
    private Long ppCountDownDelayTime;
    @NacosValue(value = "${order.snapshot.add.switch:true}", autoRefreshed = true)
    private boolean orderSnapshotAddSwitch;
    /**
     * 倒计时间延迟到权益发放后
     */
    public Long getCountDownDelayTime(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return countDownDelayTime;
        }
        if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppCountDownDelayTime;
        }
        return touchuCountDownDelayTime;
    }

    /**
     * mechat api
     */
    public String getChatterApiUrl() {
        return chatterApiUrl;
    }
    /**
     * 系统默认头像
     */
    public String getDefaultPortraitUrl(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return defaultPortraitUrl;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppDefaultPortraitUrl;
        }
        return touchuDefaultPortraitUrl;
    }

    /**
     * 获取默认信息
     */
    public String getDefaultChatMsg(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return touchuDefaultChatMsg;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppDefaultChatMsg;
        }
        return defaultChatMsg;
    }
    /**
     * 获取model发送的flashChat消息
     */
    public String getModelSendFlashChatMsg(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return modelSendFlashChatMsg;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppModelSendFlashChatMsg;
        }
        return touchuModelSendFlashChatMsg;
    }
    /**
     * 获取用户发送的flashChat消息
     */
    public String getUserFlashChatMsg(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return userSendFlashChatMsg;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppUserSendFlashChatMsg;
        }
        return touchuUserSendFlashChatMsg;
    }
    /**
     * 获取客服号码
     */
    public String getCustomerServiceUserCode(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return customerServiceUserCode;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppCustomerServiceUserCode;
        }
        return touchuCustomerServiceUserCode;
    }
    /**
     * 获取消息限制条数
     */
    public Integer getCustomerChatLimitNum(String appCode) {
        return unLimitChatMessageNum;
    }

    /**
     * flashchat消息
     */
    public String getFlashchatMessages(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return flashchatMessages;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppFlashchatMessages;
        }
        return touchuFlashchatMessages;
    }
    /**
     * ppv控制
     */
    public String getPPVSwitch(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return ppAndPvSwitch;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppPpAndPvSwitch;
        }
        return touchuPpAndPvSwitch;
    }
    /**
     * turnOns控制
     */
    public String getTurnOnsSwitch(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return turnOnsSwitch;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppTurnOnsSwitch;
        }
        return touchuTurnOnsSwitch;
    }

    /**
     * 是否启用后台配置
     */
    public Boolean getSwitchManageAdmin(String appCode) {
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return switchManageAdmin;
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppSwitchManageAdmin;
        }
        return touchuSwitchManageAdmin;
    }

    /**
     * 获取协议
     */
    public Map<String, String> getAgreement(String appCode) {
        final HashMap<String, String> agreements = Maps.newHashMap();
        if (AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            agreements.put("privacyPolicy", privacyPolicyUrl);
            agreements.put("terms", terms);
            agreements.put("paymentAgreement", paymentAgreement);
        } else if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            agreements.put("privacyPolicy", ppPrivacyPolicyUrl);
            agreements.put("terms", ppTerms);
            agreements.put("paymentAgreement", ppPaymentAgreement);
        } else if (AppCodeEnum.MEBOO_IOS.getAppCode().equals(appCode)) {
            agreements.put("privacyPolicy", iosPrivacyPolicyUrl);
            agreements.put("terms", iosTerms);
            agreements.put("paymentAgreement", iosPaymentAgreement);
        }
        else {
            agreements.put("privacyPolicy", touchuPrivacyPolicyUrl);
            agreements.put("terms", touchuTerms);
            agreements.put("paymentAgreement", touchuPaymentAgreement);
        }
        return agreements;
    }

    /**
     * 获取登陆方式
     */
    public String getLoginChannel(String appCode, String appVersion) {

        if (AppCodeEnum.TOUCHU.getAppCode().equals(appCode)) {
            return touchuLoginChannel;
        }

        if (AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return ppLoginChannel;
        }

        // 若未配置，默认采用google登陆
        if (Integer.parseInt(appVersion) > 50) {
            return loginChannelNew;
        }

        return StringUtils.isEmpty(loginChannel) ? "google" : loginChannel;
    }

    public JSONArray getHomePageBgm(String appCode) {
        if(AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return JSONObject.parseArray(homepageBgm);
        }
        if(AppCodeEnum.TOUCHU.getAppCode().equals(appCode)) {
            return JSONObject.parseArray(touchuHomepageBgm);
        }
        if(AppCodeEnum.PP.getAppCode().equals(appCode)) {
            return JSONObject.parseArray(ppHomepageBgm);

        }
        return JSONObject.parseArray(homepageBgm);
    }

    public Boolean getOrderSnapshotAddSwitch(String appCode) {
        if(AppCodeEnum.MEBOO.getAppCode().equals(appCode)) {
            return orderSnapshotAddSwitch;
        }
//        if(AppCodeEnum.TOUCHU.getAppCode().equals(appCode)) {
//        }
//        if(AppCodeEnum.PP.getAppCode().equals(appCode)) {
//        }
        return orderSnapshotAddSwitch;
    }
}
